package com.yuanrui.wechat.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信消息工厂
 */
@Component
public class WechatMsgFactory implements InitializingBean {

    @Resource
    private List<WechatMsgHandler> wechatMsgHandlers;

    private final Map<WechatMsgTypeEnum, WechatMsgHandler> WechatMsgHandlerCache=new HashMap<>();

    public WechatMsgHandler getWechatMsgHandler(String msgType) {
        WechatMsgTypeEnum wechatMsgTypeEnum = WechatMsgTypeEnum.getEnum(msgType);
        return WechatMsgHandlerCache.get(wechatMsgTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WechatMsgHandler wechatMsgHandler : wechatMsgHandlers) {
            WechatMsgHandlerCache.put(wechatMsgHandler.getMsgType(), wechatMsgHandler);
        }
    }
}
